package com.hula.onion.book.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.config.JedisUtils;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.config.SysConstants;
import com.hula.onion.book.server.dao.IDiscoverDao;
import com.hula.onion.book.server.dao.IPlatformInfoDao;
import com.hula.onion.book.server.model.News;
import com.hula.onion.book.server.service.PlatformInfoService;
import com.hula.onion.book.server.vo.PlatformInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * Created by shan on 18/1/24.
 */
@Service
public class PlatformInfoServiceImpl implements PlatformInfoService {

    @Autowired
    private IPlatformInfoDao IPlatformInfoDao;

    @Autowired
    private IDiscoverDao discoverDao;

    @Override
    public String getPlatformList(String platformName) {
        List<PlatformInfoVo> list = IPlatformInfoDao.getPlatformList(platformName);
        CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.SUCCESS);
        commonResponse.setData(list);
        return JSONObject.toJSONString(commonResponse);
    }

    @Override
    public PlatformInfoVo getPlatformInfoById(int id) {

        String redisKey = SysConstants.REDIS_KEY_PRE + SysConstants.PLATFORM_INFO + id;

        String platformInfoStr = JedisUtils.getInstance().get(redisKey);

        if (StringUtils.isEmpty(platformInfoStr)) {

            PlatformInfoVo vo = IPlatformInfoDao.getPlatformById(id);
            if (vo != null) {
                JedisUtils.getInstance().set(redisKey, JSONObject.toJSONString(vo));
            }

            return vo;

        } else {
            return JSONObject.parseObject(platformInfoStr, PlatformInfoVo.class);
        }

    }

    @Override
    public CommonResponse getNewsList(int page, int size) {
        return new CommonResponse(discoverDao.getNewsList(page, size));
    }

    @Override
    public CommonResponse getNewsDetail(int newsId) {
        return new CommonResponse(discoverDao.getNewsDetail(newsId));
    }
}
